// This file replicates mean sales of the least-selling products as a function of scope (e.g Figure 1B)

* Arguments:
* - Includes: Directory for saving output files.
* - country: Specific country for the analysis.
args Includes country

***
// Processing Actual Data

// Load the dataset generated in PNC-within-country-data-permutation
use data_by_country.dta, clear
rename exportrank rank

// Keep only the lowest-ranked products, treating products beyond rank 6 as a single group.
keep if nprod<7|rank==nprod
replace rank=6 if rank>6
replace nprod =6 if nprod>6

// Calculate the mean log value of sales by destination, rank, and product count.
collapse (mean) lvalue, by(d rank nprod)
// Tag the dataset source for identification after combining datasets.
gen source = "PMC_by_country_data"
// Save the processed dataset for actual data.
save data_by_country_1B.dta,replace

**

//Processing Simulated Data (By Country)

// Load the dataset generated in PNC-within-country-data-permutation
use PMC_only_by_country.dta,clear
// Apply the same processing as for actual data to the simulated dataset.
keep if nprod<7|rank==nprod
replace rank=6 if rank>6
replace nprod =6 if nprod>6
collapse (mean) lvalue, by(d rank nprod permutation)
collapse (mean) lvalue, by(d rank nprod )
gen source = "PMC_by_country_sim"
// Save the processed simulated by country datase
save PMC_only_by_country_1B.dta,replace

***
//Processing Simulated Data (Global)

// Load the simulated data generated in PMC-world-data-permutation.do
use PMC_only_world.dta,clear

// Repeat the processing steps for the global simulated data.
keep if nprod<7|rank==nprod
replace rank=6 if rank>6
replace nprod =6 if nprod>6
collapse (mean) lvalue, by(d rank nprod permutation)
collapse (mean) lvalue, by(d rank nprod )
gen source = "PMC_world_sim"
// Save the processed global simulated dataset.
save PMC_only_world_1B.dta,replace

// Combine the 3 datasets
use data_by_country_1B.dta,clear
append using PMC_only_by_country_1B.dta
append using PMC_only_world_1B.dta

// Preserve the current dataset for generating graphs for the top export destinations.
preserve
	// Load dataset containing the most popular export destinations.
	use exporters_at_dest.dta,clear
	levelsof d, local(pop_dest)
	// Set specific country names for China or use destiantions names availiable in the file
	if "`country'"=="China"{
							local country_names "Hong_Kong  Japan Singapore Korea Taiwan  United_Kingdom  Germany Canada US Australia"
                            }
							else{
							    local country_names "`pop_dest'"
				    		}

// Restore the main dataset.
restore

local j =1
// Loop through each popular destination to generate mean sales of the least-selling products for exporters of different scope
foreach i of local pop_dest {
							display "`i'"
							local name: word `j' of `country_names'
			
							// least-selling
							twoway (connected lvalue nprod if (source=="PMC_by_country_data" & rank ==nprod), msymbol(s)) (connected lvalue nprod if (source=="PMC_world_sim" & rank ==nprod) , msymbol(d)) (connected lvalue nprod if (source=="PMC_by_country_sim" & rank ==nprod), msymbol(t)) if d=="`i'", caption("`name'")  ytitle(Log of Sales) xtitle(Scope) legend(order(1 "Data" 2 "PMC"  3 "PMC (within destination)")) xlabel(1 2 3 4 5 6 "6+", noticks)
	
							graph export "`Includes'\least_selling-`name'-`i'.pdf", as(pdf) name("Graph") replace

							local j=`j'+1
						  }

						  
erase data_by_country_1B.dta
erase PMC_only_by_country_1B.dta
erase PMC_only_world_1B.dta